<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/28
 * Time: 18:23
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\News;
use app\common\model\Photo;
use think\facade\Filesystem;
use think\facade\View;
use think\facade\Db;


class Info extends AdminBase
{
    public function index(){
        if ($this->request->isPost()) {
            $param = input('post.');
            unset($param['file']);
            $id = Db::name('news')->where('id',3)->value('id');
            if($id){
                $news = News::find($id);
            }else{
                $news = new News();
            }

            $param['pic'] = str_replace(fullpageurl(),'',$param['pic']);
            $msg = $news->allowfield([])->save($param);
            if($msg){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }

        $list = Db::name('news')->where('id', 3)->field('content,pic,video')->find();
        return View::fetch('index', ['list' => $list]);
    }

    public function picture(){
        // 获取表单上传文件
        $files = request()->file('file');
        foreach($files as $file){
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);
        }

        if($savename){
            return \GuzzleHttp\json_encode(['code'=>1,'path'=> '/'.$savename]);
        }else{
            return \GuzzleHttp\json_encode(['code'=>0,'msg'=>'上传失败']);
        }
    }
}